package day7;

import java.util.Random;
import java.util.Scanner;

/**
 * 需求：生成四位的验证码（大写字母，小写字母，数字）
 */
public class VerificationCode {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String randomCode = generateVerificationCode(4);
        System.out.println(randomCode);
        System.out.println("请输入验证码：");
        String code = sc.next();
        judgingRandomNumber(randomCode,code);
    }

    public static String  generateVerificationCode(int length){
        Random r = new Random();
        String code = "";
        char[] chs ={'a','b','c','d','e','f','g','h','i','j',
                     'k','l','m','n','o','p','q','r','s','t',
                     'u','v','w','x','y','z','A','B','C','D',
                     'E','F','G','H','I','J','K','L','M','N',
                     'O','P','Q','R','S','T','U','V','W','X',
                     'Y','Z','0','1','2','3','4','5','6','7',
                     '8','9'} ;
        for (int i = 0; i < length; i++) {
            int index = r.nextInt(chs.length);
            code+=chs[index];

        }


        return code;
    }

    public static void judgingRandomNumber(String randomCode,String code){
        if (randomCode.equalsIgnoreCase(code)){
            System.out.println("验证码正确");
        }else {
            System.out.println("验证码错误");
        }
    }
}
